<?php
/*****************************************
 This file is part of the Dynamo Core CMS
 Copyright (c) Dynamo Studios 2008

 *** NOTE ***
 Permission is hereby granted to execute this file as a
 web service under the terms of the license agreement.

 See LICENSE.txt, in the system folder.

 All other rights, other than those expressed in the license
 document, are reserved.

 ****************************************/

/**
 * @name Dynamo CMS Core With API
 * @version 0.1
 * @abstract
 * Provides access to the standard CMS core, with some extra
 * API routines to make the programmer's life easier.
 *
 * @author Sean Micklethwaite
 */

require_once('dycms.php');

function processCmsCommands(cUiPage& $page, $site = 'dycms'){
    if($_GET['$action'] == 'view')
    {
    	$obj = dyLoadObject($_GET['$clsid'], $_GET['$id']);

    	if(!($obj instanceof IDycmsObject))
    	{
    		$queryCls = STDCLS_PAGE;
    		$z_queryArgs = array('name'=>'404');
    		$runQuery = true;
    	};
    }
    else if($_GET['$action'] == 'page')
    {
    	$queryCls = STDCLS_PAGE;
    	$z_queryArgs = array('site'=> $site,'name' => urldecode($_GET['name']));
    	$runQuery = true;
    }
    else if($_GET['$action'] == 'browse')
    {
    	//
    	// Decode arguments
    	//

    	$z_queryArgs = Array();
    	foreach($_GET as $s_idx => $s_val)
    	{
    		if(isset($_GET['$clsid']))
    			$queryCls = $_GET['$clsid'];

    		if($s_idx[0] !== '$')
    			$z_queryArgs[urldecode($s_idx)] = urldecode($s_val);
    	}

    	$runQuery = true;
    }
    else if($_GET['$action'] == 'edit')
    {
    	if(isset($_GET['$id']))
    		$obj = dyLoadObject($_GET['$clsid'], $_GET['$id']);
    	else
    		$obj = dyCreateObject($_GET['$clsid']);

    	if(!($obj instanceof cDycmsObject))
    	{
    		$queryCls = STDCLS_PAGE;
    		$z_queryArgs = array('name'=>'404');
    		$runQuery = true;
    	}
    	else
    		$edit = true;
    }
    else
    {
    	$queryCls = STDCLS_PAGE;
    	$z_queryArgs = array('site' => $site, 'name'=>'index');
    	$runQuery = true;
    };

    if($runQuery)
    {
    	do
    	{
    		$obj = new cDycmsQuery();

    		if($queryCls)
    			$obj->SetClass($queryCls);
    		if($z_queryArgs)
    			$obj->AddArray($z_queryArgs);

    		$n = $obj->Execute();

    		if(($n === 0) && ($z_queryArgs != array('name'=>'404')))
    		{
    			$z_queryArgs = array('name'=>'404');
    			$queryCls = STDCLS_PAGE;
    		}
    		else
    			break;
    	}while(true);
    };

    if($obj instanceof IDycmsObject)
    {
    	if($edit)
    	{
    		$page->sTitle = 'Editing - '.$obj->GetSummary();
    		$obj->Edit($page);
    	}
    	else
    	{
    		$page->sTitle = $obj->GetSummary();
    		$obj->Render($page->ContentNode());
    	}
    };
};

?>
